cmake_minimum_required(VERSION 3.6)

# 查找源文件的根目录
STRING(LENGTH "${sgbd_root_dir}" sgbd_root_dir_length)
if (${sgbd_root_dir_length} EQUAL 0)
    set(sgbd_root_dir ${CMAKE_CURRENT_LIST_DIR}/)
endif()

# 目录分组
macro(CC_SOURCE_GROUP_BY_DIR source_files)       
	set(sgbd_cur_dir ${CMAKE_CURRENT_SOURCE_DIR}/)
	foreach(sgbd_file ${${source_files}})
        string(REGEX REPLACE ${sgbd_root_dir} "" sgbd_repath ${sgbd_file} )
		string(REGEX REPLACE ${sgbd_cur_dir} "" sgbd_fpath ${sgbd_repath})
		string(REGEX REPLACE "[^/\\\\]+$" "" sgbd_group_name ${sgbd_fpath})
		if(sgbd_group_name)
			string(REPLACE "/"   "\\\\"  sgbd_group_name_normal ${sgbd_group_name})
			source_group(${sgbd_group_name_normal} FILES ${sgbd_file})
        endif(sgbd_group_name)
	endforeach(sgbd_file)
endmacro(CC_SOURCE_GROUP_BY_DIR)

# 采集源代码
macro (CC_COLLECT_COMPLIE_SOURCE in_soruce_root_dir out_source_file_list)
    file(GLOB ${out_source_file_list}
        ${in_soruce_root_dir}/*.cpp
        ${in_soruce_root_dir}/*.c 
        ${in_soruce_root_dir}/*.cc
        ${in_soruce_root_dir}/*.hh
        ${in_soruce_root_dir}/*.hpp 
        ${in_soruce_root_dir}/*.h
        ${in_soruce_root_dir}/*.mm
        ${in_soruce_root_dir}/*.m
    )
endmacro (CC_COLLECT_COMPLIE_SOURCE)

# 遍历采集源代码
macro (CC_COLLECT_COMPLIE_SOURCE_RECURSE in_soruce_root_dir out_source_file_list)
    file(GLOB_RECURSE ${out_source_file_list}
        ${in_soruce_root_dir}/*.cpp
        ${in_soruce_root_dir}/*.c 
        ${in_soruce_root_dir}/*.cc
        ${in_soruce_root_dir}/*.hh
        ${in_soruce_root_dir}/*.hpp 
        ${in_soruce_root_dir}/*.h
        ${in_soruce_root_dir}/*.mm
        ${in_soruce_root_dir}/*.m
    )
endmacro (CC_COLLECT_COMPLIE_SOURCE_RECURSE)

# 采集源代码，并根据目录分组
macro(all_source CC_SOURCES)
    CC_COLLECT_COMPLIE_SOURCE(${CMAKE_CURRENT_LIST_DIR} cc_complie_sources)
    CC_SOURCE_GROUP_BY_DIR(cc_complie_sources)
    list(APPEND ${CC_SOURCES} ${cc_complie_sources})    
endmacro(all_source)

# 遍历采集源代码，并根据目录分组
macro(all_source_recurse CC_SOURCES)
    CC_COLLECT_COMPLIE_SOURCE_RECURSE(${CMAKE_CURRENT_LIST_DIR} cc_complie_sources)
    CC_SOURCE_GROUP_BY_DIR(cc_complie_sources)
    list(APPEND ${CC_SOURCES} ${cc_complie_sources})    
endmacro(all_source_recurse)

# 打印文件目录
macro(print_files files)
    set(sgbd_cur_dir ${CMAKE_CURRENT_SOURCE_DIR}/)
    foreach(sgbd_file ${${files}})
        string(REGEX REPLACE ${sgbd_root_dir} "" sgbd_repath ${sgbd_file} )
        message(${sgbd_repath})
    endforeach(sgbd_file)
endmacro(print_files)

macro(test_project extr)
    file(GLOB_RECURSE CC_SOURCES
        ${CMAKE_CURRENT_LIST_DIR}/*.cpp
        ${CMAKE_CURRENT_LIST_DIR}/*.c 
        ${CMAKE_CURRENT_LIST_DIR}/*.cc
    )

    foreach(sgbd_file ${CC_SOURCES})
        # 使用正则表达式提取文件名
        if (${extr} STREQUAL ".cpp")
            string(REGEX MATCH "[^/\\\\]+(\.cpp)$" file_name ${sgbd_file})
            string(REGEX REPLACE "\.cpp" "" extracted_file_name ${file_name})
            # message(extracted_file_name:${extracted_file_name})
            add_executable(${extracted_file_name} ${sgbd_file})
        elseif(${extr} STREQUAL ".c")
            string(REGEX MATCH "[^/\\\\]+(\.c)$" file_name ${sgbd_file})
            string(REGEX REPLACE "\.c" "" extracted_file_name ${file_name})
            # message(extracted_file_name:${extracted_file_name})
            add_executable(${extracted_file_name} ${sgbd_file})
        endif(${extr} STREQUAL ".cpp")
    endforeach(sgbd_file)
endmacro(test_project)

# D:/dev-code/vscode/boostLearn/src/system/main.cpp




